
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Tom
 */
public class ImageProcessing {
    
    //__KONSTANTY
    final int IMG_HEIGHT = 256;
    final int IMG_WIDTH = 256;
    static final int NUM_OF_IMAGES = 113;
    static final int FINAL_IMG_WIDTH = 128;
    static final int FINAL_IMG_HEIGHT = 128;
    
    // rezy lebky
    BufferedImage[] images;

    
    public ImageProcessing(){
        loadImages();
  //      trans2OpenglCoords();
        setImages2FinalDimension();
    }
    
    
    public BufferedImage[] getImages(){
        return images;
    }
    
    /*
     * Převede velikost obrazku do finalni podoby 128x128x113,
     * průměrováním sousedních pixelů
     * 
     */
    private void setImages2FinalDimension(){
        for(int z = 0; z< NUM_OF_IMAGES; z++){
          BufferedImage img = new BufferedImage(FINAL_IMG_WIDTH, FINAL_IMG_HEIGHT, 
                    BufferedImage.TYPE_USHORT_GRAY);
          for(int x = 0; x<IMG_WIDTH-2; x+=2){
            for(int y = 0; y<IMG_WIDTH-2; y+=2){
                //prumerna barva sousednich pixelu
                int diameterColor = images[z].getRaster().getSample(x, y, 0) +
                                    images[z].getRaster().getSample(x+1, y+1, 0);
                diameterColor = (int)(diameterColor/2);
                        
                img.getRaster().setSample(x/2, y/2, 0, diameterColor);
            }
          }
          images[z] = img;
        }
    }
    
    
    /*
     * Prevede souradnice obrazku do souradnic OpenGL
     * 
     */
    private void trans2OpenglCoords(){
      BufferedImage[] tmpImages = images.clone();
      float tmpColor;
      for(int z = 0; z<NUM_OF_IMAGES; z++){
        for(int x = 0; x<IMG_WIDTH; x++){
           for(int y = 0; y<IMG_HEIGHT; y++){
               //  x, -y + FINAL_IMG_HEIGHT, -z + NUM_OF_IMAGES
              tmpColor = images[-z + NUM_OF_IMAGES - 1].getRaster().getSample(x, -y + IMG_HEIGHT-1, 0);
              tmpImages[z].getRaster().setSample(x, y, 0, tmpColor);
           }
         }
       }
      images = tmpImages;
    }
    
    
    /*
     * 
     * Nahraje rezy lebky do pripraveneho pole
     * 
     */
   private void loadImages(){
        images = new BufferedImage[NUM_OF_IMAGES];
        
        String number;
        for(int i = 0; i<NUM_OF_IMAGES; i++){
            if(i<9){ 
                number = "00"+ Integer.toString(i+1);
            } else if(i < 99){
                number = "0"+ Integer.toString(i+1);
            } else{
                number = Integer.toString(i+1);
            }
          try {
            File file = new File("./cthead/cthead-16bit"  + number + ".png");
            images[i] = ImageIO.read(file);
           } catch (IOException ex) {
               Logger.getLogger(ImageProcessing.class.getName()).log(Level.SEVERE, null, ex);
           }
        }
    }
    
}
